package jmine.tec.report.impl.table;

import java.io.Serializable;

import jmine.tec.report.api.table.ReportCell;
import jmine.tec.report.api.table.ReportTableGroupCell;

/**
 * Implementação de ReportTableGroup para células que tem o valor relacionado com o primeiro ou último valor do conjunto de registros.
 * 
 * @author lundberg
 * @param <V>
 */
public class ReportTableGroupDynamicCell<V> implements ReportTableGroupCell<V>, Serializable {

    private final CellValueResolver<V> resolver;

    private final ReportCell cell;

    private Object lastResolved;

    /**
     * Construtor
     * 
     * @param cell cell
     * @param resolver resolver
     */
    public ReportTableGroupDynamicCell(ReportCell cell, CellValueResolver<V> resolver) {
        this.cell = cell;
        this.resolver = resolver;
    }

    /**
     * {@inheritDoc}
     */
    public ReportCell createCell() {
        return this.cell.copy(this.lastResolved);
    }

    /**
     * {@inheritDoc}
     */
    public void processRecord(V record) {
        this.lastResolved = this.resolver.resolveCellValue(record);
    }
}
